using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Common;
using Common.NotifyEvent;
using Define;
using SProject.Program;
using WeakEvent;

namespace device
{
	[Serializable]
	public class Device : NotNotifyPropertyChanged
	{
		public static string DeviceModel_96P = "SLAN-96P";

		public static string DeviceModel_96S = "SLAN-96S";

		public static string DeviceModel_48P = "SLAN-48P";

		public static string DeviceModel_24P = "SLAN-24P";

		public static string DeviceModel_40P = "SLAN-40P";

		protected DeviceManager deviceManager = DeviceManager.GetInstance();

		[NonSerialized]
		private SynchronizationContext _synchronizationContext = SynchronizationContext.Current;

		private FastSmartWeakEvent<EventHandler> _RawDataChangedEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _RunMeltingSegmentEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _SaveTempFileEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _BlockTemperatureChangeEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _ModelChangedEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _RunStateEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _RunErrorEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _OperationEnableEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _StatusUpdateEventEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _CheckOrderEventEvent = new FastSmartWeakEvent<EventHandler>();

		private FastSmartWeakEvent<EventHandler> _RunTemperatureUpdateEvent = new FastSmartWeakEvent<EventHandler>();

		private int _Company;

		private string _Model;

		private string _Series;

		private string _PortName;

		private bool _PreTemperatureArrive;

		private bool _PreHeatIsStop;

		private int _CommandStartNo = -1;

		private string _BreakPoint;

		private double _CurrentBlockTemperature;

		private double _CurrentTubeTemperature;

		private double _CurrentHotlidTemperature;

		private string _Running;

		private int _Cycles;

		private double _SetTemperature;

		private int _HoldingTimeMinute;

		private int _HoldingTimeSecond;

		private TimeSpan _RunningTime;

		private TimeSpan _RemainTime;

		private RunningError _RunError;

		private RunningState _RunState;

		private bool _OperationEnable = true;

		private List<double[,]> _MeltingTemperatureData;

		private List<double> _ScanStartTemperature;

		private List<RunTemperature> _RunTemperatureList = new List<RunTemperature>();

		private DateTime _StartTime;

		private DateTime _StopTime;

		[NonSerialized]
		private TubeCalculator _TubeCalculator;

		public bool hotlidTemperatureArrived;

		public double OldHotlidTemperature;

		public bool IsDeleteSegment;

		public bool IsAddCycles;

		public int AddCycles;

		public RunCommand lastTemperatureCommand = CommandCreator.Command_Temperature(3, "", "", 0, -1.0, 0, 1, false, 0.0, false, false);

		public List<StepSpeed> stepSpeedList = new List<StepSpeed>();

		public List<int> EstimateRunTimeList = new List<int>();

		public Curve EstimateTimeErrorCurve = new Curve();

		private bool _RunningSegIsMelting;

		internal double OverTemperature = 7.0;

		internal double ArriveThreshold = 2.0;

		private static double speedUp = 4.6;

		private static double speedDown = 3.2;

		private static double stopThreshold = 0.0;

		private static double overPredict = 6.9;

		private int Version { get; set; }

		public int Company
		{
			get
			{
				return _Company;
			}
			set
			{
				_Company = value;
			}
		}

		public string Model
		{
			get
			{
				return _Model;
			}
			set
			{
				if (!(_Model == value))
				{
					_Model = value;
					OnModelChangedEvent();
					OnPropertyChanged("Model");
				}
			}
		}

		public string DispalyModel
		{
			get
			{
				if (string.IsNullOrEmpty(Model))
				{
					return string.Empty;
				}
				string message = Utility.GetMessage(Model);
				if (string.IsNullOrEmpty(message))
				{
					return Model;
				}
				return message;
			}
		}

		public string Series
		{
			get
			{
				return _Series;
			}
			set
			{
				if (!(_Series == value))
				{
					_Series = value;
					OnPropertyChanged("Series");
				}
			}
		}

		public string PortName
		{
			get
			{
				return _PortName;
			}
			set
			{
				if (!(_PortName == value))
				{
					_PortName = value;
					OnPropertyChanged("PortName");
				}
			}
		}

		public bool Fast { get; set; }

		public byte UsedBlock { get; set; }

		public byte UsedHotlid { get; set; }

		public bool HotlidIsHeating { get; set; }

		public int HotlidSetTemperature { get; set; }

		public byte TemperatureControl { get; set; }

		public bool IsFastControl
		{
			get
			{
				return TemperatureControl == 2;
			}
		}

		public double Volume { get; set; }

		public byte ScanMethod { get; set; }

		public byte ScanChannel { get; set; }

		public bool Compensator { get; set; }

		public byte[] ChannelNo { get; set; }

		public byte[] DarkCurrent { get; set; }

		public List<byte[]> Calibration { get; set; }

		public int OpenHotlidDelayTime { get; set; }

		public bool IsIncludePreheatSegment { get; set; }

		public bool IsCheckOrder { get; set; }

		public bool PreTemperatureArrive
		{
			get
			{
				return _PreTemperatureArrive;
			}
			set
			{
				if (_PreTemperatureArrive != value)
				{
					_PreTemperatureArrive = value;
					OnOperationEnableEvent();
				}
			}
		}

		public bool PreHeatIsStop
		{
			get
			{
				return _PreHeatIsStop;
			}
			set
			{
				if (_PreHeatIsStop != value)
				{
					_PreHeatIsStop = value;
				}
			}
		}

		public int CommandStartNo
		{
			get
			{
				return _CommandStartNo;
			}
			set
			{
				if (_CommandStartNo != value)
				{
					_CommandStartNo = value;
					OnSaveTempFileEvent();
					OnPropertyChanged("CommandStartNo");
				}
			}
		}

		public string BreakPoint
		{
			get
			{
				return _BreakPoint;
			}
			set
			{
				if (!(_BreakPoint == value))
				{
					_BreakPoint = value;
					OnPropertyChanged("BreakPoint");
				}
			}
		}

		public PCRProgram CurrentProfile { get; set; }

		public List<RunCommand> CommandList { get; set; }

		public double CurrentBlockTemperature
		{
			get
			{
				return _CurrentBlockTemperature;
			}
			set
			{
				if (_CurrentBlockTemperature != value)
				{
					_CurrentBlockTemperature = value;
					OnPropertyChanged("CurrentBlockTemperature");
				}
			}
		}

		public double CurrentTubeTemperature
		{
			get
			{
				return _CurrentTubeTemperature;
			}
			set
			{
				if (_CurrentTubeTemperature != value)
				{
					_CurrentTubeTemperature = value;
					OnPropertyChanged("CurrentTubeTemperature");
				}
			}
		}

		public double CurrentHotlidTemperature
		{
			get
			{
				return _CurrentHotlidTemperature;
			}
			set
			{
				if (_CurrentHotlidTemperature != value)
				{
					_CurrentHotlidTemperature = value;
					OnPropertyChanged("CurrentHotlidTemperature");
				}
			}
		}

		public string Running
		{
			get
			{
				return _Running;
			}
			set
			{
				if (!(_Running == value))
				{
					_Running = value;
					OnPropertyChanged("Running");
				}
			}
		}

		public int Cycles
		{
			get
			{
				return _Cycles;
			}
			set
			{
				if (_Cycles != value)
				{
					_Cycles = value;
					OnPropertyChanged("Cycles");
				}
			}
		}

		public double SetTemperature
		{
			get
			{
				return _SetTemperature;
			}
			set
			{
				if (_SetTemperature != value)
				{
					_SetTemperature = value;
					OnPropertyChanged("SetTemperature");
				}
			}
		}

		public int HoldingTimeMinute
		{
			get
			{
				return _HoldingTimeMinute;
			}
			set
			{
				if (_HoldingTimeMinute != value)
				{
					_HoldingTimeMinute = value;
					OnPropertyChanged("HoldingTimeMinute");
				}
			}
		}

		public int HoldingTimeSecond
		{
			get
			{
				return _HoldingTimeSecond;
			}
			set
			{
				if (_HoldingTimeSecond != value)
				{
					_HoldingTimeSecond = value;
					OnPropertyChanged("HoldingTimeSecond");
				}
			}
		}

		public TimeSpan RunningTime
		{
			get
			{
				return _RunningTime;
			}
			set
			{
				if (!(_RunningTime == value))
				{
					_RunningTime = value;
					OnPropertyChanged("RunningTime");
				}
			}
		}

		public TimeSpan RemainTime
		{
			get
			{
				return _RemainTime;
			}
			set
			{
				if (!(_RemainTime == value))
				{
					_RemainTime = value;
					OnPropertyChanged("RemainTime");
				}
			}
		}

		public RunningError RunError
		{
			get
			{
				return _RunError;
			}
			set
			{
				_RunError = value;
				OnRunErrorEvent();
				OnPropertyChanged("RunError");
			}
		}

		public RunningState RunState
		{
			get
			{
				return _RunState;
			}
			set
			{
				if (_RunState != value)
				{
					_RunState = value;
					OnRunStateEvent();
					OnPropertyChanged("RunState");
				}
			}
		}

		public bool OperationEnable
		{
			get
			{
				return _OperationEnable;
			}
			set
			{
				if (_OperationEnable != value)
				{
					_OperationEnable = value;
					OnOperationEnableEvent();
					OnPropertyChanged("OperationEnable");
				}
			}
		}

		public List<FluorescenceData[,]> RawData { get; set; }

		public List<FluorescenceData[,]> RawMeltingData { get; set; }

		public List<double[,]> MeltingTemperatureData
		{
			get
			{
				if (_MeltingTemperatureData == null)
				{
					_MeltingTemperatureData = new List<double[,]>();
				}
				return _MeltingTemperatureData;
			}
			set
			{
				if (_MeltingTemperatureData != value)
				{
					_MeltingTemperatureData = value;
					OnPropertyChanged("MeltingTemperatureData");
				}
			}
		}

		public List<double> ScanStartTemperature
		{
			get
			{
				if (_ScanStartTemperature == null)
				{
					_ScanStartTemperature = new List<double>();
				}
				return _ScanStartTemperature;
			}
			set
			{
				if (_ScanStartTemperature != value)
				{
					_ScanStartTemperature = value;
					OnPropertyChanged("ScanStartTemperature");
				}
			}
		}

		public List<RunTemperature> RunTemperatureList
		{
			get
			{
				return _RunTemperatureList;
			}
			set
			{
				if (_RunTemperatureList != value)
				{
					_RunTemperatureList = value;
					OnPropertyChanged("RunTemperatureList");
				}
			}
		}

		public DateTime StartTime
		{
			get
			{
				return _StartTime;
			}
			set
			{
				if (!(_StartTime == value))
				{
					_StartTime = value;
					OnPropertyChanged("StartTime");
				}
			}
		}

		public DateTime StopTime
		{
			get
			{
				return _StopTime;
			}
			set
			{
				if (!(_StopTime == value))
				{
					_StopTime = value;
					OnPropertyChanged("StopTime");
				}
			}
		}

		public TubeCalculator TubeCalculator
		{
			get
			{
				if (_TubeCalculator == null)
				{
					_TubeCalculator = new TubeCalculator();
				}
				return _TubeCalculator;
			}
			set
			{
				if (_TubeCalculator != value)
				{
					_TubeCalculator = value;
					OnPropertyChanged("TubeCalculator");
				}
			}
		}

		public RunAbnormityLog RunAbnormityLog { get; set; }

		public bool RunningSegIsMelting
		{
			get
			{
				return _RunningSegIsMelting;
			}
			set
			{
				if (_RunningSegIsMelting != value)
				{
					_RunningSegIsMelting = value;
					OnPropertyChanged("RunningSegIsMelting");
				}
			}
		}

		public event EventHandler RawDataChangedEvent
		{
			add
			{
				_RawDataChangedEvent.Add(value);
			}
			remove
			{
				_RawDataChangedEvent.Remove(value);
			}
		}

		public event EventHandler RunMeltingSegmentEvent
		{
			add
			{
				_RunMeltingSegmentEvent.Add(value);
			}
			remove
			{
				_RunMeltingSegmentEvent.Remove(value);
			}
		}

		public event EventHandler SaveTempFileEvent
		{
			add
			{
				_SaveTempFileEvent.Add(value);
			}
			remove
			{
				_SaveTempFileEvent.Remove(value);
			}
		}

		public event EventHandler BlockTemperatureChangeEvent
		{
			add
			{
				_BlockTemperatureChangeEvent.Add(value);
			}
			remove
			{
				_BlockTemperatureChangeEvent.Remove(value);
			}
		}

		public event EventHandler ModelChangedEvent
		{
			add
			{
				_ModelChangedEvent.Add(value);
			}
			remove
			{
				_ModelChangedEvent.Remove(value);
			}
		}

		public event EventHandler RunStateEvent
		{
			add
			{
				_RunStateEvent.Add(value);
			}
			remove
			{
				_RunStateEvent.Remove(value);
			}
		}

		public event EventHandler RunErrorEvent
		{
			add
			{
				_RunErrorEvent.Add(value);
			}
			remove
			{
				_RunErrorEvent.Remove(value);
			}
		}

		public event EventHandler OperationEnableEvent
		{
			add
			{
				_OperationEnableEvent.Add(value);
			}
			remove
			{
				_OperationEnableEvent.Remove(value);
			}
		}

		public event EventHandler StatusUpdateEvent
		{
			add
			{
				_StatusUpdateEventEvent.Add(value);
			}
			remove
			{
				_StatusUpdateEventEvent.Remove(value);
			}
		}

		public event EventHandler CheckOrderEvent
		{
			add
			{
				_CheckOrderEventEvent.Add(value);
			}
			remove
			{
				_CheckOrderEventEvent.Remove(value);
			}
		}

		public event EventHandler RunTemperatureUpdateEvent
		{
			add
			{
				_RunTemperatureUpdateEvent.Add(value);
			}
			remove
			{
				_RunTemperatureUpdateEvent.Remove(value);
			}
		}

		public void OnRawDataChanged()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseDataChangedEvent, new EventArgs());
			}
		}

		private void RaiseDataChangedEvent(object param)
		{
			_RawDataChangedEvent.Raise(this, param as EventArgs);
		}

		public void OnRunMeltingSegment()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseRunMeltingSegmentEvent, new EventArgs());
			}
		}

		private void RaiseRunMeltingSegmentEvent(object param)
		{
			_RunMeltingSegmentEvent.Raise(this, param as EventArgs);
		}

		public void OnSaveTempFileEvent()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseSaveTempFileEvent, new EventArgs());
			}
		}

		private void RaiseSaveTempFileEvent(object param)
		{
			_SaveTempFileEvent.Raise(this, param as EventArgs);
		}

		public void OnBlockTemperatureChangeEvent()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseBlockTemperatureChangeEvent, new EventArgs());
			}
		}

		private void RaiseBlockTemperatureChangeEvent(object param)
		{
			_BlockTemperatureChangeEvent.Raise(this, param as EventArgs);
		}

		public void OnModelChangedEvent()
		{
			if (IsNotSyncContext())
			{
				_synchronizationContext.Send(RaiseModelChangedEvent, new EventArgs());
			}
			else
			{
				RaiseModelChangedEvent(new EventArgs());
			}
		}

		private void RaiseModelChangedEvent(object param)
		{
			_ModelChangedEvent.Raise(this, param as EventArgs);
		}

		public void OnRunStateEvent()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseRunStateEvent, new EventArgs());
			}
		}

		private void RaiseRunStateEvent(object param)
		{
			_RunStateEvent.Raise(this, param as EventArgs);
		}

		public void OnRunErrorEvent()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseRunErrorEvent, new EventArgs());
			}
		}

		private void RaiseRunErrorEvent(object param)
		{
			_RunErrorEvent.Raise(this, param as EventArgs);
		}

		public void OnOperationEnableEvent()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseOperationEnableEvent, new EventArgs());
			}
		}

		private void RaiseOperationEnableEvent(object param)
		{
			_OperationEnableEvent.Raise(this, param as EventArgs);
		}

		public void OnStatusUpdateEvent()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseStatusUpdateEvent, new EventArgs());
			}
		}

		private void RaiseStatusUpdateEvent(object param)
		{
			_StatusUpdateEventEvent.Raise(this, param as EventArgs);
		}

		public void OnCheckOrderEvent()
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseCheckOrderEvent, new EventArgs());
			}
		}

		private void RaiseCheckOrderEvent(object param)
		{
			_CheckOrderEventEvent.Raise(this, param as EventArgs);
		}

		public void OnRunTemperatureUpdateEvent(RunTemperature temp)
		{
			if (_synchronizationContext != null)
			{
				_synchronizationContext.Post(RaiseRunTemperatureEvent, new RunTemperatureEventArgs
				{
					RunTemperature = temp
				});
			}
		}

		private void RaiseRunTemperatureEvent(object param)
		{
			_RunTemperatureUpdateEvent.Raise(this, param as EventArgs);
		}

		private bool IsNotSyncContext()
		{
			if (_synchronizationContext != null)
			{
				return _synchronizationContext != SynchronizationContext.Current;
			}
			return false;
		}

		public Device()
		{
			DeviceDisplayInit();
			RunError = RunningError.None;
			RunState = RunningState.NoRunning;
			RawData = new List<FluorescenceData[,]>();
			RawMeltingData = new List<FluorescenceData[,]>();
			MeltingTemperatureData = new List<double[,]>();
			ScanStartTemperature = new List<double>();
			ChannelNo = new byte[6];
			DarkCurrent = new byte[6];
			Calibration = new List<byte[]>();
			CommandStartNo = -1;
			BreakPoint = Utility.GetMessage("device_initialize");
			RunAbnormityLog = new RunAbnormityLog();
			CommandList = new List<RunCommand>();
		}

		public void DeviceDisplayInit()
		{
			CurrentBlockTemperature = 0.0;
			CurrentTubeTemperature = 0.0;
			CurrentHotlidTemperature = 0.0;
			Running = Utility.GetMessage("device_not_running");
			Cycles = 0;
			SetTemperature = 0.0;
			HoldingTimeMinute = 0;
			HoldingTimeSecond = 0;
			RunningTime = new TimeSpan(0, 0, 0);
			RemainTime = new TimeSpan(0, 0, 0);
			OnStatusUpdateEvent();
		}

		public void ProgramRun()
		{
			OperationEnable = false;
			CommandList.Insert(1, CommandCreator.Command_CloseHotlid(UsedHotlid, HotlidSetTemperature, HotlidIsHeating));
		}

		public void ProgramRunAfterCheckOrder()
		{
			RawData.Clear();
			CommandStartNo++;
			OperationEnable = false;
			CommandList.Insert(1, CommandCreator.Command_Null());
		}

		public void Run(bool isBreakPoint = true)
		{
			if (IsFastControl)
			{
				OverTemperature = ConfigHide.GetInstance().OverTemperature;
				ArriveThreshold = ConfigHide.GetInstance().ArriveThreshold;
			}
			PCRCommandExplain();
			if (IsFastControl && HotlidIsHeating)
			{
				HotlidSetTemperature = 95;
			}
			hotlidTemperatureArrived = false;
			OldHotlidTemperature = 0.0;
			OperationEnable = false;
			RawMeltingData.Clear();
			MeltingTemperatureData.Clear();
			ScanStartTemperature.Clear();
			RunState = RunningState.WaitingRunning;
			RunError = RunningError.None;
			lastTemperatureCommand.CMD_SetTemperature = -1.0;
			if (!isBreakPoint)
			{
				CommandStartNo = -1;
				BreakPoint = Utility.GetMessage("device_initialize");
				PreHeatIsStop = false;
				PreTemperatureArrive = false;
				_TubeCalculator = new TubeCalculator();
			}
			if (CommandStartNo == -1)
			{
				StartTime = DateTime.Now;
				StopTime = DateTime.Now;
				RawData.Clear();
				RunTemperatureList.Clear();
				EstimateRunTimeList.Clear();
				CommandStartNo = 0;
			}
			else
			{
				if (CommandStartNo == 0)
				{
					if (PreHeatIsStop)
					{
						PreTemperatureArrive = true;
					}
					else
					{
						PreTemperatureArrive = false;
					}
				}
				if (CommandStartNo > 0)
				{
					PreHeatIsStop = true;
					PreTemperatureArrive = true;
				}
				RunAbnormityLog.BreakpointRun_Time.Add((int)(DateTime.Now - StartTime).TotalSeconds);
				int num = -1;
				int num2 = 0;
				for (int i = 0; i < CommandList.Count; i++)
				{
					if (CommandList[i].RunCommandNo == 8 && CommandList[i].isMeltingSegment)
					{
						num = i - 3;
						break;
					}
				}
				num2 = ((num != -1 && CommandStartNo >= num) ? num : CommandStartNo);
				if (IsIncludePreheatSegment && PreHeatIsStop)
				{
					for (int num3 = 4; num3 >= 0; num3--)
					{
						if (CommandList[num3].RunCommandNo == 6 || CommandList[num3].RunCommandNo == 7 || CommandList[num3].CMD_Segment == Utility.GetMessage("preheatsection"))
						{
							CommandList.RemoveAt(num3);
						}
					}
					CommandList.Insert(2, CommandCreator.Command_CloseHotlid(UsedHotlid, HotlidSetTemperature, HotlidIsHeating));
				}
				for (int j = 0; j < num2; j++)
				{
					CommandList.RemoveAt(3);
				}
			}
			RemainTime = RemainRunTimeCal();
			deviceManager.WaitRunningDeviceList.Add(this);
			RunningSegIsMelting = false;
		}

		public void Interrupt()
		{
			RunState = RunningState.WaitingInterrupt;
			deviceManager.StopRunningDeviceList.Add(this);
			OperationEnable = false;
		}

		protected virtual void PCRCommandExplain()
		{
			IsIncludePreheatSegment = CurrentProfile.IsContainsPreHeatSegment();
			if (Model == DeviceModel_48P || Model == DeviceModel_24P || Model == DeviceModel_96S || ((Model == DeviceModel_96P || Model == DeviceModel_40P) && UsedBlock == 1))
			{
				UsedHotlid = 1;
			}
			if ((Model == DeviceModel_96P || Model == DeviceModel_40P) && UsedBlock == 2)
			{
				UsedHotlid = 2;
			}
			if ((Model == DeviceModel_96P || Model == DeviceModel_40P) && UsedBlock == 3)
			{
				UsedHotlid = 3;
			}
			CommandList.Clear();
			CommandList.Add(CommandCreator.Command_Null());
			if (IsCheckOrder)
			{
				if ((UsedHotlid & 1) != 0)
				{
					CommandList.Add(CommandCreator.Command_OpenHotlidA());
				}
				if ((UsedHotlid & 2) != 0)
				{
					CommandList.Add(CommandCreator.Command_OpenHotlidB());
				}
				CommandList.Add(CommandCreator.Command_Start(UsedBlock, ScanChannel, Compensator, IsFastControl));
				CommandList.Add(CommandCreator.Command_FluorescenceTest(UsedBlock));
				CommandList[CommandList.Count - 1].CMD_Segment = Utility.GetMessage("SampleCheckOrder");
			}
			if (IsIncludePreheatSegment)
			{
				if ((UsedHotlid & 1) != 0)
				{
					CommandList.Add(CommandCreator.Command_OpenHotlidA());
				}
				if ((UsedHotlid & 2) != 0)
				{
					CommandList.Add(CommandCreator.Command_OpenHotlidB());
				}
			}
			else
			{
				CommandList.Add(CommandCreator.Command_CloseHotlid(UsedHotlid, HotlidSetTemperature, HotlidIsHeating));
			}
			CommandList.Add(CommandCreator.Command_Start(UsedBlock, ScanChannel, Compensator, IsFastControl));
			foreach (BaseSegment program in CurrentProfile.Programs)
			{
				if (program is PreHeatSegment && ((PreHeatSegment)program).IsUse)
				{
					CommandList.Add(CommandCreator.Command_Temperature(UsedBlock, Utility.GetMessage("preheatsection"), "", 1, ((PreHeatSegment)program).Temperature, 0, 0, false, 0.0, false, false, CurrentProfile.DeltT));
				}
				if (program is Segment)
				{
					for (int i = 1; i <= ((Segment)program).Cycles; i++)
					{
						foreach (Step item in ((Segment)program).Items)
						{
							double num = item.Temperature;
							int num2 = item.HoldMinute * 60 + item.HoldSecond;
							if (item.IsAdvProgram && i >= item.StartCycle)
							{
								if (item.IsTempVarUp)
								{
									num += (double)(i - item.StartCycle + 1) * item.TempVarValue;
									if (num > 99.0)
									{
										num = 99.0;
									}
								}
								else
								{
									num -= (double)(i - item.StartCycle + 1) * item.TempVarValue;
									if (num < 4.0)
									{
										num = 4.0;
									}
								}
								if (item.IsTimeVarUp)
								{
									num2 += (i - item.StartCycle + 1) * item.TimeVarValue;
									if (num2 > 5999)
									{
										num2 = 5999;
									}
								}
								else
								{
									num2 -= (i - item.StartCycle + 1) * item.TimeVarValue;
									if (num2 < 0)
									{
										num2 = 0;
									}
								}
							}
							int holdingTimeMinute = num2 / 60;
							int holdingTimeSecond = num2 % 60;
							if (IsFastControl)
							{
								double num3 = 0.0;
								RunCommand runCommand = CommandList.Where((RunCommand s) => s.RunCommandNo == 8).LastOrDefault();
								if (runCommand != null)
								{
									if (num >= runCommand.CMD_SetTemperature + 5.0)
									{
										num3 = OverTemperature;
									}
									if (num <= runCommand.CMD_SetTemperature - 5.0)
									{
										num3 = -1.0 * OverTemperature;
									}
								}
								if (num3 != 0.0)
								{
									CommandList.Add(CommandCreator.Command_Temperature(UsedBlock, ((Segment)program).Name, item.Name, i, num, holdingTimeMinute, holdingTimeSecond, false, 0.0, false, false, CurrentProfile.DeltT + num3));
									CommandList[CommandList.Count - 1].Mark = 1;
								}
							}
							CommandList.Add(CommandCreator.Command_Temperature(UsedBlock, ((Segment)program).Name, item.Name, i, num, holdingTimeMinute, holdingTimeSecond, item.IsAcquisition, item.IsUseRamp ? item.Ramp : 0.0, false, false, CurrentProfile.DeltT));
							if (item.IsAcquisition)
							{
								CommandList.Add(CommandCreator.Command_FluorescenceTest(UsedBlock));
							}
							if (i == 1)
							{
								stepSpeedList.Add(new StepSpeed(((Segment)program).Name, item.Name));
							}
						}
					}
				}
				if (!(program is MeltingSegment))
				{
					continue;
				}
				if (((MeltingSegment)program).ScanMode == 1)
				{
					double num4 = ((MeltingSegment)program).StartTemp;
					int num5 = 0;
					for (; num4 <= ((MeltingSegment)program).EndTemp; num4 += ((MeltingSegment)program).Interval)
					{
						num5++;
						CommandList.Add(CommandCreator.Command_Temperature(UsedBlock, ((MeltingSegment)program).Name, "", num5, num4, 0, ((MeltingSegment)program).HoldTime, true, 0.0, false, true, CurrentProfile.DeltT));
						CommandList.Add(CommandCreator.Command_FluorescenceTest(UsedBlock));
					}
					stepSpeedList.Add(new StepSpeed(((MeltingSegment)program).Name, ""));
				}
				else
				{
					CommandList.Add(CommandCreator.Command_Temperature(UsedBlock, ((MeltingSegment)program).Name, "", 1, ((MeltingSegment)program).StartTemp, 0, 1, false, 0.0, false, true, CurrentProfile.DeltT));
					CommandList.Add(CommandCreator.Command_Temperature(UsedBlock, ((MeltingSegment)program).Name, "", 1, ((MeltingSegment)program).EndTemp, 0, 1, true, ((MeltingSegment)program).Rate, true, true, CurrentProfile.DeltT));
					CommandList.Add(CommandCreator.Command_FluorescenceTest(UsedBlock));
					stepSpeedList.Add(new StepSpeed(((MeltingSegment)program).Name, ""));
				}
			}
			CommandList.Add(CommandCreator.Command_Stop(UsedBlock));
			CommandList[CommandList.Count - 1].CMD_HoldingTime_Second = OpenHotlidDelayTime;
			if ((UsedHotlid & 1) != 0)
			{
				CommandList.Add(CommandCreator.Command_OpenHotlidA());
			}
			if ((UsedHotlid & 2) != 0)
			{
				CommandList.Add(CommandCreator.Command_OpenHotlidB());
			}
			CommandList.Add(CommandCreator.Command_Stop(UsedBlock));
		}

		public bool RemoveCurrentSegment(string segName)
		{
			if (CommandList.Count <= 3)
			{
				return false;
			}
			int num = CommandList[0].CMD_Cycles;
			if (CommandList[0].CMD_Segment != segName)
			{
				return false;
			}
			for (int i = 0; CommandList[i].CMD_Segment == segName && CommandList[i].CMD_Cycles == num; i++)
			{
				if (CommandList[i + 1].RunCommandNo == 9)
				{
					num--;
					break;
				}
			}
			foreach (BaseSegment program in CurrentProfile.Programs)
			{
				if (program is Segment && ((Segment)program).Name == segName)
				{
					if (num > 0)
					{
						((Segment)program).Cycles = num;
					}
					else
					{
						CurrentProfile.Programs.Remove(program);
					}
					break;
				}
			}
			IsDeleteSegment = true;
			return true;
		}

		public bool AddCurrentSegmentCycles(string segName, int n)
		{
			if (CommandList.Count <= 3)
			{
				return false;
			}
			if (CommandList[0].CMD_Segment != segName)
			{
				return false;
			}
			foreach (BaseSegment program in CurrentProfile.Programs)
			{
				if (program is Segment && ((Segment)program).Name == segName)
				{
					((Segment)program).Cycles = ((Segment)program).Cycles + n;
					break;
				}
			}
			AddCycles = n;
			IsAddCycles = true;
			return true;
		}

		public string GetCurrentSegmentName()
		{
			if (CommandList.Count > 0 && CommandList[0].RunCommandNo == 8)
			{
				return CommandList[0].CMD_Segment;
			}
			return null;
		}

		public static List<RS232Port> GetRS232PortInfo()
		{
			DeviceManager.GetInstance().RS232PortRefresh();
			return DeviceManager.GetInstance().RS232PortList;
		}

		public List<byte> SaveDevice()
		{
			List<byte> list = new List<byte>();
			List<int> list2 = new List<int>();
			List<int> list3 = new List<int>();
			List<List<byte>> list4 = new List<List<byte>>();
			list2.Add(1000);
			list2.Add(1001);
			list2.Add(1002);
			list2.Add(1023);
			list2.Add(1024);
			list2.Add(1003);
			list2.Add(1004);
			list2.Add(1005);
			list2.Add(1006);
			list2.Add(1007);
			list2.Add(1008);
			list2.Add(1009);
			list2.Add(1010);
			list2.Add(1011);
			list2.Add(1012);
			list2.Add(1013);
			list2.Add(1014);
			if (Calibration.Count == 6)
			{
				list2.Add(1015);
			}
			list2.Add(1016);
			list2.Add(1017);
			list2.Add(1018);
			list2.Add(1019);
			list2.Add(1025);
			list2.Add(1020);
			list2.Add(1026);
			list2.Add(1021);
			list2.Add(1022);
			if (RawData.Count > 0)
			{
				list2.Add(2000);
			}
			if (RawMeltingData.Count > 0 && MeltingTemperatureData.Count > 0)
			{
				list2.Add(2001);
				list2.Add(2002);
			}
			if (ScanStartTemperature.Count > 0)
			{
				list2.Add(2003);
			}
			if (RunTemperatureList.Count > 0)
			{
				list2.Add(2004);
			}
			if (EstimateRunTimeList.Count > 0)
			{
				list2.Add(2005);
			}
			if (RunAbnormityLog.DeviceConnectError_Time.Count > 0)
			{
				list2.Add(3000);
			}
			if (RunAbnormityLog.CommunicationError_Time.Count > 0)
			{
				list2.Add(3001);
			}
			if (RunAbnormityLog.CloseHotlidError_Time.Count > 0)
			{
				list2.Add(3002);
			}
			if (RunAbnormityLog.SliderNotClose_Time.Count > 0)
			{
				list2.Add(3003);
			}
			if (RunAbnormityLog.OpenHotlidError_Time.Count > 0)
			{
				list2.Add(3004);
			}
			if (RunAbnormityLog.SendCommandError_Time.Count > 0)
			{
				list2.Add(3005);
			}
			if (RunAbnormityLog.BlockOverTemperatureError_Time.Count > 0)
			{
				list2.Add(3006);
			}
			if (RunAbnormityLog.HotlidOverTemperatureError_Time.Count > 0)
			{
				list2.Add(3007);
			}
			if (RunAbnormityLog.HotlidTemperatureRiseError_Time.Count > 0)
			{
				list2.Add(3008);
			}
			if (RunAbnormityLog.FluorescenceDataError_Cycles.Count > 0)
			{
				list2.Add(3009);
			}
			if (RunAbnormityLog.FluorescenceDataError_FiberNo.Count > 0)
			{
				list2.Add(3010);
			}
			if (RunAbnormityLog.TemperatureDataError_Value.Count > 0)
			{
				list2.Add(3011);
			}
			if (RunAbnormityLog.TemperatureDataError_Time.Count > 0)
			{
				list2.Add(3012);
			}
			if (RunAbnormityLog.TimerOvertimeError_Interval.Count > 0)
			{
				list2.Add(3013);
			}
			if (RunAbnormityLog.TimerOvertimeError_Time.Count > 0)
			{
				list2.Add(3014);
			}
			if (RunAbnormityLog.BreakpointRun_Time.Count > 0)
			{
				list2.Add(3015);
			}
			if (RunAbnormityLog.CommandFinishButRS232Busy_Time.Count > 0)
			{
				list2.Add(3016);
			}
			list2.Add(-1);
			list2.Add(-1);
			for (int i = 0; i < list2.Count; i++)
			{
				list3.Add(-1);
			}
			List<byte> list5 = new List<byte>();
			byte[] array = null;
			Version = 20141113;
			list5.AddRange(BytesConverter.GetBytes(Version));
			list4.Add(list5);
			list5 = new List<byte>();
			array = BytesConverter.GetBytes(Model);
			list5.AddRange(BytesConverter.GetBytes(array.Length));
			list5.AddRange(array);
			list4.Add(list5);
			list5 = new List<byte>();
			array = BytesConverter.GetBytes(Series);
			list5.AddRange(BytesConverter.GetBytes(array.Length));
			list5.AddRange(array);
			list4.Add(list5);
			list5 = new List<byte>();
			array = BytesConverter.GetBytes(Company);
			list5.AddRange(array);
			list4.Add(list5);
			list5 = new List<byte>();
			array = BytesConverter.GetBytes(Fast);
			list5.AddRange(array);
			list4.Add(list5);
			list5 = new List<byte>();
			array = BytesConverter.GetBytes(PortName);
			list5.AddRange(BytesConverter.GetBytes(array.Length));
			list5.AddRange(array);
			list4.Add(list5);
			list5 = new List<byte>();
			list5.Add(UsedBlock);
			list4.Add(list5);
			list5 = new List<byte>();
			list5.Add(UsedHotlid);
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(HotlidIsHeating));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(HotlidSetTemperature));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(TemperatureControl));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(Volume));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.Add(ScanMethod);
			list4.Add(list5);
			list5 = new List<byte>();
			list5.Add(ScanChannel);
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(Compensator));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(ChannelNo);
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(DarkCurrent);
			list4.Add(list5);
			if (Calibration.Count == 6)
			{
				list5 = new List<byte>();
				for (int j = 0; j < 6; j++)
				{
					list5.AddRange(Calibration[j]);
				}
				list4.Add(list5);
			}
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(OpenHotlidDelayTime));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes((int)RunError));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes((int)RunState));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(StartTime));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(StartTime.ToString("yyyyMMddHHmmss"), true));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(StopTime));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(StopTime.ToString("yyyyMMddHHmmss"), true));
			list4.Add(list5);
			list5 = new List<byte>();
			list5.AddRange(BytesConverter.GetBytes(CommandStartNo));
			list4.Add(list5);
			list5 = new List<byte>();
			array = BytesConverter.GetBytes(BreakPoint);
			list5.AddRange(BytesConverter.GetBytes(array.Length));
			list5.AddRange(array);
			list4.Add(list5);
			int count = RawData.Count;
			if (count > 0)
			{
				list5 = new List<byte>();
				list5.AddRange(BytesConverter.GetBytes(count));
				for (int k = 0; k < count; k++)
				{
					for (int l = 0; l < 96; l++)
					{
						for (int m = 0; m < 6; m++)
						{
							list5.Add(RawData[k][l, m].Char1);
							list5.Add(RawData[k][l, m].Char2);
							list5.Add(RawData[k][l, m].Char3);
							list5.Add(RawData[k][l, m].Char4);
						}
					}
				}
				list4.Add(list5);
			}
			count = ((RawMeltingData.Count > MeltingTemperatureData.Count) ? MeltingTemperatureData.Count : RawMeltingData.Count);
			if (count > 0)
			{
				list5 = new List<byte>();
				list5.AddRange(BytesConverter.GetBytes(count));
				for (int n = 0; n < count; n++)
				{
					for (int num = 0; num < 96; num++)
					{
						for (int num2 = 0; num2 < 6; num2++)
						{
							list5.Add(RawMeltingData[n][num, num2].Char1);
							list5.Add(RawMeltingData[n][num, num2].Char2);
							list5.Add(RawMeltingData[n][num, num2].Char3);
							list5.Add(RawMeltingData[n][num, num2].Char4);
						}
					}
				}
				list4.Add(list5);
				list5 = new List<byte>();
				list5.AddRange(BytesConverter.GetBytes(count));
				for (int num3 = 0; num3 < count; num3++)
				{
					for (int num4 = 0; num4 < 96; num4++)
					{
						for (int num5 = 0; num5 < 6; num5++)
						{
							list5.AddRange(BytesConverter.GetBytes((float)MeltingTemperatureData[num3][num4, num5]));
						}
					}
				}
				list4.Add(list5);
			}
			count = ScanStartTemperature.Count;
			if (count > 0)
			{
				list5 = new List<byte>();
				list5.AddRange(BytesConverter.GetBytes(count));
				for (int num6 = 0; num6 < count; num6++)
				{
					list5.AddRange(BytesConverter.GetBytes(ScanStartTemperature[num6]));
				}
				list4.Add(list5);
			}
			count = RunTemperatureList.Count;
			if (count > 0)
			{
				list5 = new List<byte>();
				list5.AddRange(BytesConverter.GetBytes(count));
				for (int num7 = 0; num7 < count; num7++)
				{
					list5.AddRange(BytesConverter.GetBytes((float)RunTemperatureList[num7].BlockTemperature));
					list5.AddRange(BytesConverter.GetBytes((float)RunTemperatureList[num7].TubeTemperature));
					list5.AddRange(BytesConverter.GetBytes((float)RunTemperatureList[num7].HotlidTemperature));
					list5.AddRange(BytesConverter.GetBytes(RunTemperatureList[num7].TimeRecord.Ticks));
				}
				list4.Add(list5);
			}
			count = EstimateRunTimeList.Count;
			if (count > 0)
			{
				list5 = new List<byte>();
				list5.AddRange(BytesConverter.GetBytes(count));
				for (int num8 = 0; num8 < count; num8++)
				{
					list5.AddRange(BytesConverter.GetBytes(EstimateRunTimeList[num8]));
				}
				list4.Add(list5);
			}
			AddListToData(RunAbnormityLog.DeviceConnectError_Time, list4);
			AddListToData(RunAbnormityLog.CommunicationError_Time, list4);
			AddListToData(RunAbnormityLog.CloseHotlidError_Time, list4);
			AddListToData(RunAbnormityLog.SliderNotClose_Time, list4);
			AddListToData(RunAbnormityLog.OpenHotlidError_Time, list4);
			AddListToData(RunAbnormityLog.SendCommandError_Time, list4);
			AddListToData(RunAbnormityLog.BlockOverTemperatureError_Time, list4);
			AddListToData(RunAbnormityLog.HotlidOverTemperatureError_Time, list4);
			AddListToData(RunAbnormityLog.HotlidTemperatureRiseError_Time, list4);
			AddListToData(RunAbnormityLog.FluorescenceDataError_Cycles, list4);
			AddListToData(RunAbnormityLog.FluorescenceDataError_FiberNo, list4);
			AddListToData(RunAbnormityLog.TemperatureDataError_Value, list4);
			AddListToData(RunAbnormityLog.TemperatureDataError_Time, list4);
			AddListToData(RunAbnormityLog.TimerOvertimeError_Interval, list4);
			AddListToData(RunAbnormityLog.TimerOvertimeError_Time, list4);
			AddListToData(RunAbnormityLog.BreakpointRun_Time, list4);
			AddListToData(RunAbnormityLog.CommandFinishButRS232Busy_Time, list4);
			count = list2.Count;
			int num9 = count * 8;
			for (int num10 = 0; num10 < count - 2; num10++)
			{
				list3[num10] = num9;
				num9 += list4[num10].Count;
			}
			for (int num11 = 0; num11 < count; num11++)
			{
				list.AddRange(BytesConverter.GetBytes(list2[num11]));
				list.AddRange(BytesConverter.GetBytes(list3[num11]));
			}
			count = list4.Count;
			for (int num12 = 0; num12 < count; num12++)
			{
				list.AddRange(list4[num12]);
			}
			return list;
		}

		public void LoadDevice(byte[] buffer)
		{
			int num = Index(buffer, 1000);
			if (num != -1)
			{
				Version = BytesConverter.BytesToInt(buffer, num);
			}
			num = Index(buffer, 1002);
			if (num != -1)
			{
				Series = BytesConverter.BytesToString(buffer, num + 4, BytesConverter.BytesToInt(buffer, num));
			}
			Model = DeviceManager.GetDeviceModel(Series);
			num = Index(buffer, 1024);
			if (num != -1)
			{
				Fast = BytesConverter.BytesToBool(buffer, num);
			}
			num = Index(buffer, 1023);
			if (num != -1)
			{
				Company = BytesConverter.BytesToInt(buffer, num);
			}
			num = Index(buffer, 1003);
			if (num != -1)
			{
				PortName = BytesConverter.BytesToString(buffer, num + 4, BytesConverter.BytesToInt(buffer, num));
			}
			num = Index(buffer, 1004);
			if (num != -1)
			{
				UsedBlock = buffer[num];
			}
			num = Index(buffer, 1005);
			if (num != -1)
			{
				UsedHotlid = buffer[num];
			}
			num = Index(buffer, 1006);
			if (num != -1)
			{
				HotlidIsHeating = BytesConverter.BytesToBool(buffer, num);
			}
			num = Index(buffer, 1007);
			if (num != -1)
			{
				HotlidSetTemperature = BytesConverter.BytesToInt(buffer, num);
			}
			num = Index(buffer, 1008);
			if (num != -1)
			{
				TemperatureControl = buffer[num];
			}
			num = Index(buffer, 1009);
			if (num != -1)
			{
				Volume = BytesConverter.BytesToDouble(buffer, num);
			}
			num = Index(buffer, 1010);
			if (num != -1)
			{
				ScanMethod = buffer[num];
			}
			num = Index(buffer, 1011);
			if (num != -1)
			{
				ScanChannel = buffer[num];
			}
			num = Index(buffer, 1012);
			if (num != -1)
			{
				Compensator = BytesConverter.BytesToBool(buffer, num);
			}
			num = Index(buffer, 1013);
			if (num != -1)
			{
				for (int i = 0; i < 6; i++)
				{
					ChannelNo[i] = buffer[num + i];
				}
			}
			num = Index(buffer, 1014);
			if (num != -1)
			{
				for (int j = 0; j < 6; j++)
				{
					DarkCurrent[j] = buffer[num + j];
				}
			}
			num = Index(buffer, 1015);
			if (num != -1)
			{
				for (int k = 0; k < 6; k++)
				{
					Calibration.Add(new byte[98]);
				}
				for (int l = 0; l < 6; l++)
				{
					for (int m = 0; m < 98; m++)
					{
						Calibration[l][m] = buffer[num + l * 98 + m];
					}
				}
			}
			num = Index(buffer, 1016);
			if (num != -1)
			{
				OpenHotlidDelayTime = BytesConverter.BytesToInt(buffer, num);
			}
			num = Index(buffer, 1017);
			if (num != -1)
			{
				_RunError = (RunningError)BytesConverter.BytesToInt(buffer, num);
			}
			num = Index(buffer, 1018);
			if (num != -1)
			{
				_RunState = (RunningState)BytesConverter.BytesToInt(buffer, num);
			}
			num = Index(buffer, 1019);
			if (num != -1)
			{
				StartTime = BytesConverter.BytesToDateTime(buffer, num);
			}
			num = Index(buffer, 1025);
			if (num != -1)
			{
				string text = BytesConverter.BytesToString(buffer, num + 4, BytesConverter.BytesToInt(buffer, num));
				if (text.Length == 14)
				{
					try
					{
						StartTime = new DateTime(int.Parse(text.Substring(0, 4)), int.Parse(text.Substring(4, 2)), int.Parse(text.Substring(6, 2)), int.Parse(text.Substring(8, 2)), int.Parse(text.Substring(10, 2)), int.Parse(text.Substring(12, 2)));
					}
					catch (Exception)
					{
					}
				}
			}
			num = Index(buffer, 1020);
			if (num != -1)
			{
				StopTime = BytesConverter.BytesToDateTime(buffer, num);
			}
			num = Index(buffer, 1026);
			if (num != -1)
			{
				string text2 = BytesConverter.BytesToString(buffer, num + 4, BytesConverter.BytesToInt(buffer, num));
				if (text2.Length == 14)
				{
					try
					{
						StopTime = new DateTime(int.Parse(text2.Substring(0, 4)), int.Parse(text2.Substring(4, 2)), int.Parse(text2.Substring(6, 2)), int.Parse(text2.Substring(8, 2)), int.Parse(text2.Substring(10, 2)), int.Parse(text2.Substring(12, 2)));
					}
					catch (Exception)
					{
					}
				}
			}
			num = Index(buffer, 1021);
			if (num != -1)
			{
				CommandStartNo = BytesConverter.BytesToInt(buffer, num);
			}
			num = Index(buffer, 1022);
			if (num != -1)
			{
				BreakPoint = BytesConverter.BytesToString(buffer, num + 4, BytesConverter.BytesToInt(buffer, num));
			}
			num = Index(buffer, 2000);
			if (num != -1)
			{
				int num2 = BytesConverter.BytesToInt(buffer, num);
				for (int n = 0; n < num2; n++)
				{
					FluorescenceData[,] array = new FluorescenceData[96, 6];
					for (int num3 = 0; num3 < 96; num3++)
					{
						for (int num4 = 0; num4 < 6; num4++)
						{
							array[num3, num4] = new FluorescenceData();
							array[num3, num4].Char1 = buffer[num + n * 2304 + num3 * 24 + num4 * 4 + 4];
							array[num3, num4].Char2 = buffer[num + n * 2304 + num3 * 24 + num4 * 4 + 5];
							array[num3, num4].Char3 = buffer[num + n * 2304 + num3 * 24 + num4 * 4 + 6];
							array[num3, num4].Char4 = buffer[num + n * 2304 + num3 * 24 + num4 * 4 + 7];
						}
					}
					RawData.Add(array);
				}
			}
			num = Index(buffer, 2001);
			if (num != -1)
			{
				int num5 = BytesConverter.BytesToInt(buffer, num);
				for (int num6 = 0; num6 < num5; num6++)
				{
					FluorescenceData[,] array2 = new FluorescenceData[96, 6];
					for (int num7 = 0; num7 < 96; num7++)
					{
						for (int num8 = 0; num8 < 6; num8++)
						{
							array2[num7, num8] = new FluorescenceData();
							array2[num7, num8].Char1 = buffer[num + num6 * 2304 + num7 * 24 + num8 * 4 + 4];
							array2[num7, num8].Char2 = buffer[num + num6 * 2304 + num7 * 24 + num8 * 4 + 5];
							array2[num7, num8].Char3 = buffer[num + num6 * 2304 + num7 * 24 + num8 * 4 + 6];
							array2[num7, num8].Char4 = buffer[num + num6 * 2304 + num7 * 24 + num8 * 4 + 7];
						}
					}
					RawMeltingData.Add(array2);
				}
			}
			num = Index(buffer, 2002);
			if (num != -1)
			{
				int num9 = BytesConverter.BytesToInt(buffer, num);
				for (int num10 = 0; num10 < num9; num10++)
				{
					double[,] array3 = new double[96, 6];
					for (int num11 = 0; num11 < 96; num11++)
					{
						for (int num12 = 0; num12 < 6; num12++)
						{
							array3[num11, num12] = BytesConverter.BytesToFloat(buffer, num + num10 * 2304 + num11 * 24 + num12 * 4 + 4);
						}
					}
					MeltingTemperatureData.Add(array3);
				}
			}
			num = Index(buffer, 2003);
			if (num != -1)
			{
				int num13 = BytesConverter.BytesToInt(buffer, num);
				for (int num14 = 0; num14 < num13; num14++)
				{
					ScanStartTemperature.Add(BytesConverter.BytesToDouble(buffer, num + num14 * 8 + 4));
				}
			}
			num = Index(buffer, 2004);
			if (num != -1)
			{
				int num15 = BytesConverter.BytesToInt(buffer, num);
				for (int num16 = 0; num16 < num15; num16++)
				{
					RunTemperature runTemperature = new RunTemperature();
					int num17 = num + 4 + num16 * 20;
					runTemperature.BlockTemperature = BytesConverter.BytesToFloat(buffer, num17);
					runTemperature.TubeTemperature = BytesConverter.BytesToFloat(buffer, num17 + 4);
					runTemperature.HotlidTemperature = BytesConverter.BytesToFloat(buffer, num17 + 8);
					runTemperature.TimeRecord = new TimeSpan(BytesConverter.BytesToLong(buffer, num17 + 12));
					RunTemperatureList.Add(runTemperature);
				}
			}
			num = Index(buffer, 2005);
			if (num != -1)
			{
				int num18 = BytesConverter.BytesToInt(buffer, num);
				for (int num19 = 0; num19 < num18; num19++)
				{
					EstimateRunTimeList.Add(BytesConverter.BytesToInt(buffer, num + num19 * 4 + 4));
				}
			}
			LoadDataToList(buffer, DeviceProperty.DeviceConnectError_Time, RunAbnormityLog.DeviceConnectError_Time);
			LoadDataToList(buffer, DeviceProperty.CommunicationError_Time, RunAbnormityLog.CommunicationError_Time);
			LoadDataToList(buffer, DeviceProperty.CloseHotlidError_Time, RunAbnormityLog.CloseHotlidError_Time);
			LoadDataToList(buffer, DeviceProperty.SliderNotClose_Time, RunAbnormityLog.SliderNotClose_Time);
			LoadDataToList(buffer, DeviceProperty.OpenHotlidError_Time, RunAbnormityLog.OpenHotlidError_Time);
			LoadDataToList(buffer, DeviceProperty.SendCommandError_Time, RunAbnormityLog.SendCommandError_Time);
			LoadDataToList(buffer, DeviceProperty.BlockOverTemperatureError_Time, RunAbnormityLog.BlockOverTemperatureError_Time);
			LoadDataToList(buffer, DeviceProperty.HotlidOverTemperatureError_Time, RunAbnormityLog.HotlidOverTemperatureError_Time);
			LoadDataToList(buffer, DeviceProperty.HotlidTemperatureRiseError_Time, RunAbnormityLog.HotlidTemperatureRiseError_Time);
			LoadDataToList(buffer, DeviceProperty.FluorescenceDataError_Cycles, RunAbnormityLog.FluorescenceDataError_Cycles);
			LoadDataToList(buffer, DeviceProperty.FluorescenceDataError_FiberNo, RunAbnormityLog.FluorescenceDataError_FiberNo);
			LoadDataToList(buffer, DeviceProperty.TemperatureDataError_Value, RunAbnormityLog.TemperatureDataError_Value);
			LoadDataToList(buffer, DeviceProperty.TemperatureDataError_Time, RunAbnormityLog.TemperatureDataError_Time);
			LoadDataToList(buffer, DeviceProperty.TimerOvertimeError_Interval, RunAbnormityLog.TimerOvertimeError_Interval);
			LoadDataToList(buffer, DeviceProperty.TimerOvertimeError_Time, RunAbnormityLog.TimerOvertimeError_Time);
			LoadDataToList(buffer, DeviceProperty.BreakpointRun_Time, RunAbnormityLog.BreakpointRun_Time);
			LoadDataToList(buffer, DeviceProperty.CommandFinishButRS232Busy_Time, RunAbnormityLog.CommandFinishButRS232Busy_Time);
		}

		private int Index(byte[] buffer, int propertyNo)
		{
			int result = -1;
			int num = -1;
			while (++num >= 0)
			{
				int num2 = BytesConverter.BytesToInt(buffer, num * 8);
				if (num2 == propertyNo)
				{
					result = BytesConverter.BytesToInt(buffer, num * 8 + 4);
					break;
				}
				if (num2 == -1)
				{
					break;
				}
			}
			return result;
		}

		private void AddListToData(IList list, List<List<byte>> data)
		{
			int count = list.Count;
			if (count <= 0)
			{
				return;
			}
			List<byte> list2 = new List<byte>();
			list2.AddRange(BytesConverter.GetBytes(count));
			for (int i = 0; i < count; i++)
			{
				if (list[i] is int)
				{
					list2.AddRange(BytesConverter.GetBytes((int)list[i]));
				}
				else if (list[i] is double)
				{
					list2.AddRange(BytesConverter.GetBytes((double)list[i]));
				}
			}
			data.Add(list2);
		}

		private void LoadDataToList(byte[] buffer, DeviceProperty properNo, IList list)
		{
			int num = Index(buffer, (int)properNo);
			if (num == -1)
			{
				return;
			}
			int num2 = BytesConverter.BytesToInt(buffer, num);
			for (int i = 0; i < num2; i++)
			{
				if (list is List<int>)
				{
					list.Add(BytesConverter.BytesToInt(buffer, num + i * 4 + 4));
				}
				else if (list is List<double>)
				{
					list.Add(BytesConverter.BytesToDouble(buffer, num + i * 8 + 4));
				}
			}
		}

		public bool HasOneData()
		{
			return RawData.Count == 1;
		}

		public bool HasEnoughData()
		{
			if (RawData.Count < 3)
			{
				return RawMeltingData.Count >= 3;
			}
			return true;
		}

		public bool HasData()
		{
			if (RawData.Count < 1)
			{
				return RawMeltingData.Count >= 1;
			}
			return true;
		}

		public static TimeSpan EstimateRunTimeCal(PCRProgram program, int openHotlidDelayTime)
		{
			Device device = new Device();
			device.Model = DeviceModel_96S;
			device.UsedBlock = 3;
			device.UsedHotlid = 1;
			device.HotlidIsHeating = true;
			device.HotlidSetTemperature = 105;
			device.TemperatureControl = 0;
			device.Volume = 30.0;
			device.ScanMethod = 0;
			device.ScanChannel = 15;
			for (int i = 0; i < 4; i++)
			{
				device.ChannelNo[i] = (byte)(i + 1);
			}
			device.OpenHotlidDelayTime = openHotlidDelayTime;
			device.CurrentProfile = program;
			device.PCRCommandExplain();
			return device.RemainRunTimeCal();
		}

		public TimeSpan RemainRunTimeCal()
		{
			double num = 0.0;
			bool flag = false;
			double num2 = lastTemperatureCommand.CMD_SetTemperature;
			string cMD_Segment = lastTemperatureCommand.CMD_Segment;
			if (num2 == -1.0)
			{
				num2 = 30.0;
			}
			foreach (RunCommand command in CommandList)
			{
				if (command.Mark != 1 && (command.RunCommandNo != 7 || !flag))
				{
					num += CommandRunTimeCal(command, num2, cMD_Segment);
					if (command.RunCommandNo == 6)
					{
						flag = true;
					}
					if (command.RunCommandNo == 8)
					{
						num2 = command.CMD_SetTemperature;
						cMD_Segment = command.CMD_Segment;
					}
				}
			}
			if (CommandList.Count > 0 && CommandList[0].RunCommandNo == -1)
			{
				num += 6.0;
			}
			return new TimeSpan((long)(num * 10000000.0));
		}

		private double CommandRunTimeCal(RunCommand rc, double lastSetTemperature, string lastSegment)
		{
			switch (rc.RunCommandNo)
			{
			case -1:
				return 0.0;
			case 4:
				return 6.0;
			case 5:
				if (HotlidIsHeating)
				{
					if (IsFastControl)
					{
						return 80.0;
					}
					return 110.0;
				}
				return 5.0;
			case 6:
			case 7:
				return 6 + OpenHotlidDelayTime;
			case 8:
				return TemperatureCommandRunTimeCal(rc, lastSetTemperature, lastSegment);
			case 9:
				return 0.0;
			case 10:
				return 0.0;
			default:
				return 0.0;
			}
		}

		private double TemperatureCommandRunTimeCal(RunCommand rc, double lastSetTemperature, string lastSegment)
		{
			double speed = GetSpeed(rc, lastSegment);
			double arrivedTime;
			if (rc.isMeltingSegment)
			{
				if (rc.isContinue)
				{
					if (speed > 0.0)
					{
						if (rc.CMD_SetTemperature <= CurrentTubeTemperature)
						{
							return 0.0;
						}
						return (rc.CMD_SetTemperature - CurrentTubeTemperature) / (speed * 0.94);
					}
					return (rc.CMD_SetTemperature - lastSetTemperature) / (rc.CMD_V * 0.94);
				}
				if (speed > 0.0)
				{
					return (rc.CMD_SetTemperature - lastSetTemperature) / speed + (double)rc.CMD_HoldingTime_Second;
				}
				arrivedTime = GetArrivedTime(rc, lastSetTemperature);
				SaveSpeed(rc, lastSetTemperature, lastSegment, arrivedTime);
				return arrivedTime + (double)rc.CMD_HoldingTime_Second;
			}
			if (speed > 0.0)
			{
				return Math.Abs(rc.CMD_SetTemperature - lastSetTemperature) / speed + (double)(rc.CMD_HoldingTime_Minute * 60) + (double)rc.CMD_HoldingTime_Second;
			}
			arrivedTime = GetArrivedTime(rc, lastSetTemperature);
			SaveSpeed(rc, lastSetTemperature, lastSegment, arrivedTime);
			return arrivedTime + (double)(rc.CMD_HoldingTime_Minute * 60) + (double)rc.CMD_HoldingTime_Second;
		}

		private double GetSpeed(RunCommand rc, string lastSegment)
		{
			double result = -1.0;
			foreach (StepSpeed stepSpeed in stepSpeedList)
			{
				if (stepSpeed.Segment == rc.CMD_Segment && stepSpeed.Step == rc.CMD_Step)
				{
					result = ((!(rc.CMD_Segment == lastSegment)) ? stepSpeed.ExternSpeed : stepSpeed.InternSpeed);
					break;
				}
			}
			return result;
		}

		private void SaveSpeed(RunCommand rc, double lastSetTemperature, string lastSegment, double time)
		{
			StepSpeed stepSpeed = stepSpeedList.Where((StepSpeed s) => s.Segment == rc.CMD_Segment && s.Step == rc.CMD_Step).FirstOrDefault();
			if (stepSpeed == null)
			{
				stepSpeed = new StepSpeed(rc.CMD_Segment, rc.CMD_Step);
				stepSpeedList.Add(stepSpeed);
			}
			double num = ((!(time < 0.001)) ? (Math.Abs(rc.CMD_SetTemperature - lastSetTemperature) / time) : 1000000.0);
			if (rc.CMD_Segment == lastSegment)
			{
				stepSpeed.InternSpeed = num;
			}
			else
			{
				stepSpeed.ExternSpeed = num;
			}
		}

		private double GetArrivedTime(RunCommand rc, double lastSetTemperature)
		{
			if (Math.Abs(rc.CMD_SetTemperature - lastSetTemperature) < 0.02)
			{
				return 0.0;
			}
			double num = 0.0;
			double num2 = ((rc.CMD_SetTemperature > lastSetTemperature) ? ((!IsFastControl) ? 3.0 : speedUp) : ((!IsFastControl) ? 2.4 : speedDown));
			double num3 = Math.Abs(rc.CMD_SetTemperature - lastSetTemperature);
			double num4 = ((num3 >= 11.0) ? num2 : ((num3 >= 6.0) ? 0.9 : ((!(num3 >= 3.0)) ? 0.25 : 0.4)));
			if (rc.CMD_V != 0.0 && num4 > rc.CMD_V)
			{
				num4 = rc.CMD_V;
			}
			double num5;
			if (!(num4 >= num2))
			{
				num5 = ((num4 >= 1.5) ? 6.0 : ((!(num4 >= 0.5)) ? 0.0 : 4.0));
			}
			else
			{
				num5 = 7.0;
				if (IsFastControl)
				{
					num5 = stopThreshold;
				}
			}
			num = ((num5 >= 1.0) ? ((num3 - num5) / num4 + (num5 - 1.0) / 0.6 + 4.0) : ((!(num3 >= 1.0)) ? (num3 / 0.15 + 2.0) : ((num3 - 1.0) / num4 + 5.0)));
			if (TemperatureControl == 1 || TemperatureControl == 2)
			{
				num = TubeControlTimeCal(lastSetTemperature, rc.CMD_SetTemperature, num);
			}
			return num;
		}

		private double TubeControlTimeCal(double begin, double end, double time)
		{
			double num = 0.0;
			if (IsFastControl)
			{
				num = (double)((begin < end) ? 1 : (-1)) * overPredict;
			}
			end += num;
			double num2 = 0.0;
			double num3 = 0.11 * Math.Abs(end - begin) / time;
			double num4 = ((!(Math.Abs(end - begin) < 0.15)) ? 0.1 : 0.05);
			bool flag = ((end >= begin) ? true : false);
			TubeCalculator tubeCalculator = new TubeCalculator();
			double num5;
			do
			{
				num5 = tubeCalculator.TubeTemperatureCal(begin, Volume, IsFastControl);
				begin = (flag ? ((!(begin >= end)) ? (begin + num3) : end) : ((!(begin <= end)) ? (begin - num3) : end));
				num2 += 0.11;
			}
			while (Math.Abs(num5 - (end - num)) > num4);
			return num2;
		}

		public void GetErrorCurve()
		{
			EstimateTimeErrorCurve.CurvePoint.Clear();
			int num = (int)(StopTime - StartTime).TotalSeconds;
			int count = EstimateRunTimeList.Count;
			for (int i = 0; i < count; i++)
			{
				Dot dot = new Dot();
				dot.X = i + 1;
				dot.Y = EstimateRunTimeList[i] - num;
				EstimateTimeErrorCurve.CurvePoint.Add(dot);
			}
		}
	}
}
